﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace mOceanWindows
{
    public partial class AdInterstitialForm : Form
    {
        public AdInterstitialForm()
        {
            InitializeComponent();
            adViewControl._interstitial = this;
        }

        public AdViewControl Control
        {
            get
            {
                return adViewControl;   
            }
        }

        public int AutoCloseInterstitialTime
        {
            get { return m_nCloseTime; }
            set{ m_nCloseTime = value; }
        }

        public Button CloseButton
        {
            get { return m_closeButton; }
            set
            {
                m_closeButton = value;
            }
        }

        public int CloseButtonShowTime
        {
            get { return m_nCloseButtonShowTime; }
            set{ m_nCloseButtonShowTime = value; }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            if (m_nCloseTime > 0)
            {
                m_allowClose = false;
                timerClose.Interval = m_nCloseTime * 1000;
                timerClose.Enabled = true;
            }

            if ( (m_nCloseButtonShowTime > 0) && (m_closeButton != null) )
            {
                m_allowClose = false;
                timerShowButton.Interval = m_nCloseButtonShowTime * 1000;
                timerShowButton.Enabled = true;
                m_closeButton.Visible = false;
            }

            if (m_closeButton != null)
            {
                Controls.Clear();
                Controls.Add(m_closeButton);
                Controls.Add(adViewControl);
                m_closeButton.Click += btnClose_Click;

            }

            adViewControl.Update();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (!m_allowClose) e.Cancel = true;
        }

        private void timerClose_Tick(object sender, EventArgs e)
        {
            timerClose.Enabled = false;

            m_allowClose = true;
            Close();
        }

        internal delegate void SafeCloseDelegate();

        internal void SafeClose()
        {
            if (InvokeRequired)
            {
                Invoke(new SafeCloseDelegate(SafeClose));
            }
            else
            {
                Close();
            }

            return;
        }
        
        private void timerShowButton_Tick(object sender, EventArgs e)
        {
            timerShowButton.Enabled = false;
            m_allowClose = true;

            if (m_closeButton != null)
            {
                m_closeButton.Visible = true;
                m_closeButton.Show();
            }
        }

        protected int m_nCloseTime = 0, m_nCloseButtonShowTime = 0;
        protected Button m_closeButton = null;
        internal bool m_allowClose = true;

        private void btnClose_Click(object sender, EventArgs e)
        {
            m_allowClose = true;
            Close();
        }
    }
}
